package com.controls.ws;

import java.util.List;

import android.content.Context;

import com.Utility.WebServiceCommunicator.WebServiceFlag;
import com.events.WebServiceCommunicatorListener;
import com.objects.Contact;
import com.objects.Recepient;

public class SendMailWSControl extends BaseWSControl {
	public SendMailWSControl(Context context,
			WebServiceCommunicatorListener listener) {
		super(context, listener);
	}

	public boolean sendMail(String accessToken, List<Recepient> recipients, String subject, 
			String attachments, String message, String audio, String video) {
		
		Contact contact = recipients.get(0).getContact();
		
		String recepients = contact.getEmailAddress();
		String accessCodes = recipients.get(0).getAccessCode();
		String recipientEmails = contact.getEmailAddress();
		String reciepientFirstNames = contact.getFirstName();
		String recipientLastNames = contact.getLastName();
		String smsCountryCodes = contact.getCountryCode() == null ? "" : contact.getCountryCode();
		String recipentSMSTextNumber = "";
		String recipientNote = "";
		
		for (int i = 1; i < recipients.size(); i++) {
				
			Recepient recepient = recipients.get(i);
			Contact ct = recepient.getContact();
			
			if(null != ct.getEmailAddress() && ct.getEmailAddress().length()>0){
				recepients += "," + ct.getEmailAddress();
			}
			if(null != recepient.getAccessCode() && recepient.getAccessCode().length()>0){
				accessCodes += "," + recepient.getAccessCode();
			}
			if(null != ct.getEmailAddress() && ct.getEmailAddress().length()>0){
				recipientEmails += "," + ct.getEmailAddress();
			}
			if(null != ct.getFirstName() && ct.getFirstName().length()>0){
				reciepientFirstNames += "," + ct.getFirstName();
			}
			if(null != ct.getLastName() && ct.getLastName().length()>0){
				recipientLastNames += "," + ct.getLastName();
			}
			if(null != ct.getCountryCode() && ct.getCountryCode().length()>0){
				smsCountryCodes += "," + ct.getCountryCode();
			}
		}
		
		StringBuffer content = new StringBuffer("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
		content.append("<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">");
		content.append("<soap:Body><SendEPackage_EXT xmlns=\"https://www.privacydatasolutions.net/\">");
		
		content.append("<Token>").append(accessToken).append("</Token>");
		content.append("<Recipients>").append(recepients).append("</Recipients>");
		content.append("<Subject>").append(subject).append("</Subject>");
		content.append("<Attachments>").append(attachments==null?"":attachments).append("</Attachments>");
		content.append("<Message>").append(message).append("</Message>");
		content.append("<ElectronicSignature>true</ElectronicSignature>");
		content.append("<AccessCode>").append(accessCodes).append("</AccessCode>");
		content.append("<Fingerprint>true</Fingerprint>");
		content.append("<SMSAlert>true</SMSAlert>");
		content.append("<AllowDownload>true</AllowDownload>");
		content.append("<AllowPrint>true</AllowPrint>");
		content.append("<AllowForward>true</AllowForward>");
		content.append("<AddWatermark>true</AddWatermark>");
		content.append("<AllowView>true</AllowView>");
		content.append("<RecipientEmailAddress>").append(recipientEmails).append("</RecipientEmailAddress>");
		content.append("<RecipientFirstName>").append(reciepientFirstNames).append("</RecipientFirstName>");
		content.append("<RecipientLastName>").append(recipientLastNames).append("</RecipientLastName>");
		content.append("<RecipientSMSCountryCode>").append(smsCountryCodes).append("</RecipientSMSCountryCode>");
		content.append("<RecipientSMSTextNumber>").append(recipentSMSTextNumber).append("</RecipientSMSTextNumber>");
		content.append("<RecipientNotes>").append(recipientNote).append("</RecipientNotes>");
		content.append("<AudioMessage>").append(audio).append("</AudioMessage>");
		content.append("<VideoMessage>").append(video).append("</VideoMessage>");
		content.append("</SendEPackage_EXT></soap:Body></soap:Envelope>");

		fetch(WebServiceFlag.SEND_MAIL, content.toString());
		return true;
	}
}
